package com.mohe.shanpao.netty;

import java.math.BigInteger;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class BigIntegerEncoder extends MessageToByteEncoder<Number> {

	@Override
	protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) throws Exception {
		BigInteger v;
		if(msg instanceof BigInteger)
		{
			v = (BigInteger)msg;
		}
		else
		{
			v = new BigInteger(String.valueOf(msg));
		}
		
		byte[] data = v.toByteArray();
		int length = data.length;
		
		out.writeByte('F'); //分隔符
		out.writeInt(length);
		out.writeBytes(data);
	}

}
